<template>
	<view class="bz-fl">
		<u-button @click="bindPopup" type="primary" text="添加分类"></u-button>
		<view v-for="(item,index) in list" class="list">
			<view class="list-item">名称:{{item.tag}}</view>
			<view :data-id="item._id" @click="getAddfl" class="btn-delete">删除</view>
		</view>
		<u-popup :show="tjfl">
			<view class="popup-fl">
				<view class="list-fl">
					<view class="list-title">分类名称:</view>
					<u--input
					  placeholder="请输入内容"
					  border="surround"
					  v-model="flv"
					></u--input>
				</view>
				<view class="btn-fl">
					<u-button @click="bindAddFl" type="warning" text="添加"></u-button>
					<u-button @click="bindPopup" type="info" text="取消"></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
import { data } from '../../uni_modules/uview-ui/libs/mixin/mixin';
	export default {
		data() {
			return {
				list:null,
				tjfl:false,
				flv:null
			}
		},
		onLoad() {
			this.getFl()
		},
		methods: {
			getFl(){
				const db = uniCloud.database();
				db.collection('bz-tag').orderBy("tim", "desc").get()
				.then(res=>{
					console.log(res.result.data,'jjjjssss')
					this.list = res.result.data
				})
			},
			getAddfl(e){
				uni.showModal({
					title:"删除",
					content:"确定删除吗？"
				}).then(res=>{
					if(res.confirm==true){
						const id = e.currentTarget.dataset.id
						const db = uniCloud.database();
						db.collection('bz-tag').doc(id).remove()
						.then(res=>{
							uni.showToast({
								icon:'success',
								title:'删除成功'
							})
							this.getFl()
						})
					}
				})
			},
			bindAddFl(){
				const tim = Date.now()
				console.log(Date.now())
				const db = uniCloud.database();
				db.collection('bz-tag').add({
					"tag":this.flv,
					"tim":tim
				}).then(res=>{
					console.log(res,'jjjjssss')
					uni.showToast({
						icon:'success',
						title:'添加成功'
					})
					this.getFl()
					this.bindPopup()
				})
			},
			bindPopup(){
				this.tjfl = !this.tjfl
			}
		}
	}
</script>

<style lang="scss">
		.bz-fl{
			padding: 15rpx;
			background: #f7f7f7;
			min-height: 100vh;
			.list{
				display: flex;
				justify-content: space-between;
				margin-top: 15rpx;
				padding: 10rpx;
				border: 2rpx solid #9e9e9e;
				border-radius: 15rpx;
				background: #fff;
				.list-item{
					padding: 8rpx;
					padding-inline-start: 15rpx;
					padding-inline-end: 15rpx;
					height: 45rpx;
					background: #00c458;
					border-radius: 18rpx;
					color: #fff;
					text-align: center;
				}
				.btn-delete{
					width: 120rpx;
					height: 60rpx;
					background: #ed2300;
					color: #fff;
					border-radius: 15rpx;
					text-align: center;
					line-height: 1.8;
				}
			}
			.popup-fl{
				padding: 45rpx 25rpx;
				.list-fl{
					display: flex;
					align-items: center;
					color: #848484;
				}
				.btn-fl{
					display: flex;
				}
			}
		}
</style>
